<?php
/*------------------------------------------------------------------------
# Search Groups
# ------------------------------------------------------------------------
# Dmitry Chernov
# Copyright (C) 2011 Provitiligo.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Website: http://www.provitiligo.com
# Support:  provitiligo@gmail.com
-------------------------------------------------------------------------*/

// no direct access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.plugin.plugin');

class plgSearchCommunityGroups extends JPlugin
{
	public function __construct(& $subject, $config)
	{
		parent::__construct($subject, $config);
		$this->loadLanguage();
	}

	function onContentSearchAreas()
	{
		static $areas = array(
			'CommunityGroups' => 'PLG_SEARCH_COMMUNITYGROUPS_MAIN'
		);
		return $areas;
	}

	function onContentSearch($text, $phrase='', $ordering='', $areas=null)
	{
		$db =& JFactory::getDBO();
		$user =& JFactory::getUser();
		$app = JFactory::getApplication();

		require_once(JPATH_SITE.DS.'components'.DS.'com_community'.DS.'libraries'.DS.'core.php');

		if (is_array($areas))
		{
			if (!array_intersect($areas, array_keys(onContentSearchAreas())))
			{
				return array();
			}
		}

		$limit = $this->params->def('search_limit', 50);

		$text = trim($text);

		if ($text == '')
		{
			return array();
		}

		$wheres = array();

		switch ($phrase)
		{
			case 'exact':
				$text = $db->Quote('%'.$db->getEscaped($text, true).'%', false);
				$wheres2 = array();
				$wheres2[] = 'LOWER(a.description) LIKE '.$text;
				$wheres2[] = 'LOWER(a.name) LIKE '.$text;
				$where = '('.implode(') OR (', $wheres2).')';
				break;
			case 'all':
			case 'any':
			default:
				$words = explode(' ', $text);
				$wheres = array();
				foreach ($words as $word)
				{
					$word = $db->Quote('%'.$db->getEscaped($word, true).'%', false);
					$wheres2 = array();
					$wheres2[] = 'LOWER(a.description) LIKE '.$word;
					$wheres2[] = 'LOWER(a.name) LIKE '.$word;
					$wheres[]= implode(' OR ', $wheres2);
				}
				$where = '('.implode(($phrase == 'all' ? ') AND (' : ') OR ('), $wheres).')';
				break;
		}

		switch ($ordering)
		{
			case 'alpha':
				$order = 'a.name ASC';
				break;
			case 'category':
				$order = 'b.name ASC';
				break;
			case 'popular':
				$order = 'a.membercount DESC';
				break;
			case 'newest':
				$order = 'a.created DESC';
				break;
			case 'oldest':
				$order = 'a.created ASC';
				break;
			default:
				$order = 'a.name DESC';
				break;
		}

		$query = 'SELECT a.id, a.name AS title, a.description AS text, b.name AS category,';
		$query .= ' CONCAT_WS("'.JText::_('PLG_SEARCH_COMMUNITYGROUPS_DELIMITER').'", "'.JText::_('PLG_SEARCH_COMMUNITYGROUPS_MAIN').'", b.name) AS section,';
		$query .= ' a.created AS created, "2" AS browsernav';
		$query .= ' FROM #__community_groups AS a';
		$query .= ' INNER JOIN #__community_groups_category AS b ON a.categoryid = b.id';
		$query .= ' WHERE '.$where;
		$query .= ' AND a.published = "1"';
		$query .= ' ORDER BY '.$order;

		$db->setQuery($query, 0, $limit);
		$results = $db->loadObjectList();

		if($results)
		{
			foreach($results as $key => $row)
			{
				$results[$key]->href = CRoute::_('index.php?option=com_community&view=groups&task=viewgroup&groupid='.$row->id);
				$results[$key]->text = $row->text;
			}
		}
		return $results;
	}
}

?>